﻿using DevExpress.Mvvm;
using DevExpress.Utils.MVVM;
using DevExpress.Utils.MVVM.Services;
using DevExpress.Utils.Taskbar;
using DevExpress.Utils.Taskbar.Core;
using DevExpress.XtraBars.Navigation;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraEditors;
using DevExpress.XtraSplashScreen;
using DotNetExtensions.AspNet.DevExpress.WIN;
using Habi.Solver.Services;
using Habi.Solver.ViewModels;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Habi.Solver
{
    public partial class FormMain : XtraForm
    {
        static FormMain()
        {
            MVVMContext.RegisterFlyoutDialogService();
        }
        public FormMain()
        {
            InitializeComponent();
            mvvmContext.ViewModelType = typeof(MainViewModel);
            mvvmContext.RegisterService("FlyoutDocumentManagerService", WindowedDocumentManagerService.CreateFlyoutFormService(this));
            mvvmContext.RegisterService(new ControlOperationService(this.navigationFrame));

            RegisterMoveEvent();

            navButtonHome.Glyph = WindowExtensions.GetImage("navButtonHome");
            navButtonMin.Glyph = WindowExtensions.GetImage("navButtonMin");
            navButtonMax.Glyph = WindowExtensions.GetImage("navButtonMax");
            navButtonInfo.Glyph = WindowExtensions.GetImage("navButtonInfo");
            navButtonHelp.Glyph = WindowExtensions.GetImage("navButtonHelp");
            navButtonClose.Glyph = WindowExtensions.GetImage("navButtonClose");

            if (!mvvmContext.IsDesignMode) InitializeNavigation();

            DevExpress.Utils.About.UAlgo.Default.DoEventObject(DevExpress.Utils.About.UAlgo.kDemo, DevExpress.Utils.About.UAlgo.pWinForms, new HybridAppWin());
        }
        void RegisterMoveEvent()
        {
            this.AddDraggableControl(new Control[] { tileNavPane });
        }
        void InitializeNavigation()
        {
            mvvmContext.RegisterService(DocumentManagerService.Create(navigationFrame));
            mvvmContext.RegisterService("FilterDialogService",DialogService.CreateFlyoutDialogService(this));

            var fluent = mvvmContext.OfType<MainViewModel>();
            fluent.SetItemsSourceBinding(tileBar, tb => tb.Groups, x => x.ModuleGroups, (group, moduleGroup) => object.Equals(group.Tag, moduleGroup),
                moduleGroup => CreateGroup(fluent, moduleGroup));
            fluent.WithEvent(this, "Load")
                .EventToCommand(x => x.OnLoaded(null), x => x.DefaultModule);
            fluent.WithEvent(this, "Closing")
                .EventToCommand(x => x.OnClosing(null));

            fluent.SetBinding(barStaticItemDriveSize, t => t.Caption, x => x.Size);
            fluent.SetBinding(barStaticItemIPAddress, t => t.Caption, x => x.IPAddress);
            fluent.SetBinding(barStaticItemMac, t => t.Caption, x => x.Mac);

            fluent.BindCommand(navButtonInfo, x => x.Info());
            fluent.BindCommand(navButtonHelp, x => x.About());
            fluent.BindCommand(navButtonClose, x => x.Quit());


            fluent.BindCommand(navButtonMin, x => x.Minimize());
            fluent.BindCommand(navButtonMax, x => x.Maximize());

            fluent.SetBinding(navButtonText, t => t.Caption, x => x.Title);

            fluent.BindCommand(barButtonItemDownloads, x => x.ShowTaskView());
            fluent.BindCommand(barButtonItemUser, x => x.ShowLoginView());


            Messenger.Default.Register<LoginUserData>(this, user =>
            {
                this.barButtonItemUser.ImageIndex = 5;
                this.barButtonItemUser.Caption = $"Sign on {user.Name}";
            });
        }
        TileBarGroup CreateGroup(MVVMContextFluentAPI<MainViewModel> fluentAPI, IGrouping<string, Module> moduleGroup)
        {
            TileBarGroup group = new TileBarGroup() { Tag = moduleGroup };
            group.Text = moduleGroup.Key.ToUpper();
            foreach (Module module in moduleGroup)
                group.Items.Add(RegisterModuleItem(fluentAPI, module));
            return group;
        }
        TileBarItem RegisterModuleItem(MVVMContextFluentAPI<MainViewModel> fluentAPI, Module module)
        {
            TileBarItem item = new TileBarItem() { Tag = module };
            item.Text = module.Title;
            item.Elements[0].ImageUri = MenuExtensions.GetImageUri(module.Icon);
            item.AppearanceItem.Normal.BackColor = module.BackColor;
            item.AppearanceItem.Normal.ForeColor = Color.White;
            item.ItemSize = TileBarItemSize.Wide;

            fluentAPI.BindCommand(item, x => x.Show(null), x => module);
            return item;
        }
        protected override FormShowMode ShowMode
        {
            get { return FormShowMode.AfterInitialization; }
        }
        protected override void OnShown(EventArgs e)
        {
            SplashScreenManager.CloseDefaultSplashScreen();
            base.OnShown(e);
        }

        public void ShowNavPanel()
        {
            tileNavPane.Visible = true;
        }

        protected override void WndProc(ref Message msg)
        {
            base.WndProc(ref msg);
            this.WndResize(ref msg, 10);
        }

    }

    internal class HybridAppWin
    {
        public HybridAppWin()
        {
        }
    }
}
